package my.soft.aiteacher.utils;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component
public class RedisIdUtils {
    @Resource
    private RedisTemplate<String, Objects> redisTemplate;

    public  Long getId(String key) {
        // 生成ID
        long timestamp = System.currentTimeMillis() / 1000;
        LocalDate date = LocalDate.now();
        String now = date.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        String keyValue  = "ids:" + key + ":" + now;
        Long increment = redisTemplate.opsForValue().increment(keyValue);
        redisTemplate.expire(keyValue, 24, TimeUnit.HOURS);
        return timestamp << 32 | increment;
    }
}
